/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.puzzud.engine.core;

import java.util.Vector;

/**
 *
 * @author puzzud
 */
public class Entity
{
    protected int id;
    protected Entity owner;
    protected int type;
    protected int state;

    protected Vector<Entity> entityList;

    public final static int TYPE_NONE   = -1;

    public Entity()
    {
        this( TYPE_NONE );
    }

    public Entity( int type )
    {
        this( type, null );
    }

    public Entity( int type, Entity owner )
    {
        entityList = new Vector<Entity>();

        setId( -1 );
        setOwner( owner );
        setType( type );
        setState( -1 );
    }

    public int getId()
    {
        return( id );
    }

    public void setId( int id )
    {
        this.id = id;
    }
    
    public Entity getOwner()
    {
        return( owner );
    }
    
    public void setOwner( Entity owner )
    {
        this.owner = owner;
    }

    public int getType()
    {
        return( type );
    }

    public void setType( int type )
    {
        this.type = type;
    }

    public int getState()
    {
        return( state );
    }

    public void setState( int state )
    {
        this.state = state;
    }

    public Vector<Entity> getEntityList()
    {
        return( entityList );
    }

    public void setEntityList( Vector<Entity> entityList )
    {
        this.entityList = entityList;
    }

    public int getEntityIndex( Entity entity )
    {
        int index;
        boolean found = false;

        for( index = 0; index < entityList.size(); index++ )
        {
            if( entityList.get( index ) == entity )
            {
                found = true;
                break;
            }
        }

        if( found == true )
        {
            return( index );
        }
        else
        {
            return( -1 );
        }
    }

    public Entity getEntity( int entityIndex )
    {
        if( entityIndex >= entityList.size() )
        {
            return( null );
        }
        else
        {
            return( entityList.get( entityIndex ) );
        }
    }

    public Entity takeEntityByIndex( int entityIndex )
    {
        Entity tempEntity;

        if( entityIndex >= entityList.size() )
        {
            return( null );
        }
        else
        {
            tempEntity = entityList.remove( entityIndex );
            return( tempEntity );
        }
    }

    public Entity takeEntity( Entity entity )
    {
        int index;
        boolean found = false;
        Entity tempEntity = null;

        for( index = 0; index < entityList.size(); index++ )
        {
            tempEntity = entityList.remove( index );

            if( tempEntity == entity )
            {
                found = true;
                break;
            }
        }

        if( found == true )
        {
            return( tempEntity );
        }
        else
        {
            return( null );
        }
    }

    public void removeItemByIndex( int entityIndex )
    {
        takeEntityByIndex( entityIndex );
    }

    public void removeEntity( Entity entity )
    {
        takeEntity( entity );
    }

    public void addEntity( Entity entity )
    {
        if( entityList.indexOf( entity ) < 0 )
        {
            entityList.add( entity );
        }
    }

    public int getStatus()
    {
        return( state );
    }

    public void setStatus( int status )
    {
        this.state = status;
    }

    public Entity getFirstEntityByType( int type )
    {
        int index;

        for( index = 0; index < entityList.size(); index++ )
        {
            Entity tempEntity = entityList.get( index );

            if( tempEntity.getType() == type )
            {
                return( tempEntity );
            }
        }

        return( null );
    }

    public Entity getFirstEntityByClass( String className )
    {
        int index;

        for( index = 0; index < entityList.size(); index++ )
        {
            Entity tempEntity = entityList.get( index );

            if( tempEntity.getType() == type )
            {
                return( tempEntity );
            }
        }

        return( null );
    }

    public int getEntityCountByType( int type )
    {
        int count, index;

        for( index = 0, count = 0; index < entityList.size(); index++ )
        {
            Entity tempEntity = entityList.get( index );

            if( tempEntity.getType() == type )
            {
                count++;
            }
        }

        return( count );
    }
/*
    @Override
    public String toString()
    {
        return( type );
    }

    @Override
    public Object clone()
    {
        Entity copiedEntity = new Entity();
        copiedEntity.setId(id);
        copiedEntity.setOwner(owner);
        copiedEntity.setStatus(state);
        copiedEntity.setType(type);
        copiedEntity.setEntityList(entityList);

        return copiedEntity;
    }
*/
}
